/*
	Written By Pradipna Nepal
	www.pradsprojects.com

	Copyright (C) 2011 Prads Projects
	Please read COPYING.txt included along with this source code for more detail.
	If not included, see http://www.gnu.org/licenses/
*/

#ifndef LIBEMUGRAPHICS_H
#define LIBEMUGRAPHICS_H

#include <d3d9.h>
#include <d3dx9.h>
#include "libFrameTimer.h"

#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")

#define CUSTFVF					(D3DFVF_XYZ | D3DFVF_TEX1)
#define RELEASE(ptr) {	\
	if (ptr) {			\
		ptr->Release();	\
		ptr = 0; } }

struct TDVertices {
	float x, y, z;
	float u, v;
};

class Graphics {
private:
	IDirect3DDevice9 *d3dDevice;
	D3DPRESENT_PARAMETERS d3dInfo;
	IDirect3DTexture9 *screen;
	IDirect3DVertexBuffer9 *vbScreen;
	D3DLOCKED_RECT lockedRect;
	void createScreen();

	FrameTimer frameTimer;

	int defaultPalette[16], usedPalette[16];	//Palettes
	unsigned char background, foreground[76800];	//Pixel Buffer
	unsigned char spriteHeight, spriteWidth;
	bool flipX, flipY;	//Sprite Flipping

	//Draw un-flipped horizontal sprite pixels line
	void drawLine(unsigned char *cMem, int addrOffset, int X, int Y, unsigned char *flag);
	//Draw un-flipped horizontal sprite pixels line
	void drawLineFliped(unsigned char *cMem, int addrOffset, int X, int Y, unsigned char *flag);

public:
	Graphics();
	~Graphics();

	bool initialize(HWND hwnd, int wWidth, int wHeight);
	
	void clearScreen();
	bool checkIfVBlank(unsigned int *cycle);
	void setBackground(unsigned char *index);
	void setSpriteSize(unsigned char *width, unsigned char *height);
	void drawSprite(unsigned char *cMem, short int X, short int Y, int addr, unsigned char *flag);
	void setFlip(bool horizontal, bool vertical);
	void loadPalette(unsigned char *cMem, int addr);
	void renderToScreen();
};

#endif